package com.learning.railwayreservation.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.learning.railwayreservation.biz.Booking;
import com.learning.railwayreservation.vo.PassengerVO;
import com.learning.railwayreservation.vo.TicketVO;

public class ConfirmBookingServlet extends HttpServlet {

	private static final long serialVersionUID = 6736640944440509102L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		int counter = 1;

		PassengerVO passengerVo;
		List<PassengerVO> passList = new ArrayList<PassengerVO>();

		System.out.println("Seat Id: "
				+ request.getParameter("USeatId_" + counter));
		while (request.getParameter("USeatId_" + counter) != null) {
			passengerVo = new PassengerVO();
			passengerVo.setFirstName(request.getParameter("FirstName_"+ counter));
			passengerVo.setLastName(request.getParameter("LastName_"+ counter));
			passengerVo.setAge(new Integer(request.getParameter("Age_"+ counter))
					.intValue());
			passengerVo.setSex(request.getParameter("Sex_"+ counter));
			passengerVo.getSeatVo().setUniqueSeatId(new Integer(request
					.getParameter("USeatId_"+ counter)).intValue());
			System.out.println(" Name: " + passengerVo.getFirstName() + " "
					+ passengerVo.getLastName());
			System.out.println(" Age: " + passengerVo.getAge());
			System.out.println(" Sex: " + passengerVo.getSex());
			System.out.println(" Seat ID: " + passengerVo.getSeatVo().getUniqueSeatId());

			passList.add(passengerVo);
			counter++;
		}
		Booking bookTickets = new Booking();
		TicketVO ticketvo = new TicketVO();
		ticketvo.setPassengerList(passList);
		TicketVO confirmBooking = bookTickets.bookTickets(ticketvo);

		request.setAttribute("seatDetails", confirmBooking);
		request.setAttribute("someInfo", "Hi My Name is Richa!!");

		 request.getRequestDispatcher("/jsp/ticket.jsp").forward(request,
		 response);

	}
}